package com.emellend.api.list;


import com.emellend.api.R;
import com.emellend.api.resource.Cheeses;

import android.app.ListActivity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
/**
 * 动态添加条目到listview
 * @author Administrator
 *
 */
public class List9 extends ListActivity implements OnScrollListener {
	
	private RemoveWindow mRemoveWindow=new RemoveWindow();
	Handler mHandler=new Handler();
	private WindowManager mWindowManager;
	private TextView mDialogText;
	private boolean mShowing;
	private boolean mReady;
	private char mPrevLetter=Character.MIN_VALUE;
	private String[] mStrings = Cheeses.sCheeseStrings;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mWindowManager=getWindowManager();
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mStrings));
		getListView().setOnScrollListener(this);
		LayoutInflater inflater=LayoutInflater.from(this);
		mDialogText=(TextView) inflater.inflate(R.layout.list_position, null);
		mDialogText.setVisibility(View.INVISIBLE);
		mHandler.post(new Runnable() {
			public void run() {
				mReady=true;
				WindowManager.LayoutParams lp=new WindowManager.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 
						WindowManager.LayoutParams.TYPE_APPLICATION, 
						WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
						PixelFormat.TRANSLUCENT);
				mWindowManager.addView(mDialogText, lp);
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		mReady=true;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mReady=false;
		removeWindow();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mWindowManager.removeView(mDialogText);
		mReady=false;
	}
	
	public void onScrollStateChanged(AbsListView view, int scrollState) {}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (mReady) {
			char firstLetter=mStrings[firstVisibleItem].charAt(0);
			if (!mShowing&&firstLetter!=mPrevLetter) {
				mShowing=true;
				mDialogText.setVisibility(View.VISIBLE);
			}
			mDialogText.setText(firstLetter+"");
			mHandler.removeCallbacks(mRemoveWindow);
			mHandler.postDelayed(mRemoveWindow, 3000);
			mPrevLetter=firstLetter;
		}
	}
	
	private final class RemoveWindow implements Runnable{
		public void run() {
			removeWindow();
		}
	}
	
	private void removeWindow() {
		if (mShowing) {
			mShowing=false;
			mDialogText.setVisibility(View.INVISIBLE);
		}
	}
}
